﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ZH.GPT.Utils
{
    /// <summary>
    /// HttpContext扩展
    /// </summary>
    public static class HttpContextExtensions
    {
        /// <summary>
        /// 获取UserId
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static Guid? UserId(this HttpContext context)
        {

            var userId = context.Request.Headers["UserId"];
            if (!string.IsNullOrEmpty(userId))
            {
                if (Guid.TryParse(userId.ToString(), out var uid))
                {
                    return uid;
                }
            }
            return null;
        }
    }
}
